package com.lwq.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.naming.Context;
import javax.swing.text.View;

import com.lwq.cocos.CCObject;
import com.lwq.controller.BaseController;

public class ReflectUtil {

	public static Object reflect(CCObject obj, String command, Class<?> cals,
			Object vals) {
		Method method = null;
		Object result = null;
		if (obj == null || command == null) {
			return null;
		}
		if (Constance.getCCClass(obj.getType()) == null)
			return null;
		try {
			method = Constance.getCCClass(obj.getType()).getMethod(command,
					cals);
			result = method.invoke(obj, vals);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return result;
		}
	}

	public static Object reflect(CCObject obj, String command) {
		Method method = null;
		Object result = null;
		if (obj == null || command == null) {
			return null;
		}
		if (Constance.getCCClass(obj.getType()) == null)
			return null;
		try {
			method = Constance.getCCClass(obj.getType()).getMethod(command);
			result = method.invoke(obj);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return result;
		}
	}
	/**
	 * 获得对象的class
	 * 
	 * @param command
	 * @param param
	 * @return
	 */
	public static Class<?> getObjClass(String command, Object param) {
		Class<?> cls = null;
		if (command.equals("setText")) {
			return Constance.CHARSEQUENCE_CLASS;
		} else {
			if (param != null) {
				cls = param.getClass();
				if (cls == Constance.INTEGER_CLASS) {
					cls = int.class;
				} else if (cls == Constance.BOOLEAN_CLASS) {
					cls = boolean.class;
				} else if (cls == Constance.FLOAT_CLASS) {
					cls = float.class;
				} else if (cls == Constance.DOUBLE_CLASS) {
					cls = double.class;
				}
			}
		}
		return cls;
	}

	/**
	 * 由class new出一个对象
	 * 
	 * @param className
	 * @param args
	 * @return
	 * @throws Exception
	 */
	public static Object newInstance(String className, Object[] args)
			throws Exception {
		Class newoneClass = Class.forName(className);
		Class[] argsClass = new Class[args.length];
		for (int i = 0, j = args.length; i < j; i++) {
			argsClass[i] = args[i].getClass();
		}
		Constructor cons = newoneClass.getConstructor(argsClass);
		return cons.newInstance(args);
	}

}
